// pages/cate/cate.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'我是分类页面',
    list:['苹果','香蕉','梨'],
    currentIndex:0,
    typeList:[
      {id:1001,typename:'新闻',content:'新闻内容'},
      { id: 1002, typename: '体育', content:'体育内容'},
      { id: 1003, typename: '娱乐', content:'娱乐内容'},
      { id: 1004, typename: '财经', content:'财经内容'},
    ],
    list2:[
      {id:1001,name:'小明',age:20},
      {id:1002,name:'小李',age:18},
      {id:1003,name:'小侯',age:19},
      {id:1004,name:'小贾',age:22},
    ],
    page:1,
    count:5,
    flag:false,
    moveList:[], //定义要渲染的电影列表变量
  },
  //tab切换
  changeFn(e) {
    let {index}=e.currentTarget.dataset;
    this.setData({
      currentIndex:index
    })


  },
  //删除
  deleteItem(e) {
    let {index}=e.currentTarget.dataset
    console.log('delete',index)

   // this.list2.splice(index,1)

   console.log(this.data.list2)

    //this.data.list2.splice(index,1)

    let currentItem ='list2['+index+'].name'

   this.setData({
    // list2: this.data.list2
     [currentItem]:'侯婕'
   })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMovieList()
  },
  getMovieList() {
    let url ='https://api.douban.com/v2/movie/top250?apikey=0b2bdeda43b5688921839c8ecb20399b'

  this.setData({
    flag:false
  })

   wx.showLoading({
     title: '正在加载中。。。',
   })

   /*
     page(页码)   start（从第几条开始）   count：每页几条
       1            0                    5
       2            5                    5
       3            10                   5
       4            15                   5

    start=(page-1)*count
   
    */

   let {page,count}=this.data
   wx.request({
      url: url,
      data:{
        start:(page-1)*count,
        count
      },
      header:{
        'content-type':'application/text'
      },
      success:(res)=> {
      //  console.log(res)
        let { statusCode,data}=res;
        let { subjects }=data;
        console.log(statusCode)
        console.log(data)
        console.log(subjects)

        if (statusCode===200) {
          page++

          this.data.moveList = this.data.moveList.concat(subjects)
          this.setData({
            moveList: this.data.moveList,
            page,
            flag:true
          })

          wx.hideLoading()
        }
      }
    })
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('触底加载')
   if(this.data.flag) {
     this.getMovieList()
   }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})